package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.R;
import com.gxa.pojo.Role;
import com.gxa.service.RoleService;
import com.gxa.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/role/edit")
public class EditRoleServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String role_num = req.getParameter("role_num");
        String role_name = req.getParameter("role_name");
        int department_id = Integer.parseInt(req.getParameterValues("department_name")[0]);
        String mark = req.getParameter("mark");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String update_time = df.format(d);

        Role role = new Role();
        role.setId(id);
        role.setRole_num(role_num);
        role.setRole_name(role_name);
        role.setDepartment_id(department_id);
        role.setUpdate_time(update_time);
        role.setMark(mark);

        RoleService roleService = new RoleServiceImpl();
        R r = roleService.updateRole(role);

        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(r));

    }
}
